package com.sczmgk.hotline_pzh.photo.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.provider.MediaStore.Images.Thumbnails;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.android.bitmapfun.util.LoadImageThumb;
import com.android.bitmapfun.util.MyImageFetcher;
import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.photo.PhotoActivity;
import com.sczmgk.hotline_pzh.photo.PhotoGridItem;
import com.sczmgk.hotline_pzh.photo.ViewPagerActivity;
import com.sczmgk.hotline_pzh.photo.entity.PhotoAlbum;
import com.sczmgk.hotline_pzh.photo.entity.PhotoItem;
import com.sczmgk.hotline_pzh.photo.entity.PictureData;
import com.sczmgk.hotline_pzh.utils.MyLog;

/**
 * 相册（greadViewAdapter）
 *
 */
public class PhotoAdappter extends BaseAdapter implements OnClickListener{

    private String TAG = PhotoAdappter.class.getSimpleName();
    private PhotoActivity mActivity;
    private PhotoAlbum aibum;
    
    public PhotoAdappter(PhotoActivity context, PhotoAlbum aibum) {
        this.mActivity = context;
        this.aibum = aibum;
    }


    @Override
    public int getCount() {
        return aibum.getBitList().size();
    }

    @Override
    public Object getItem(int position) {
        return aibum.getBitList().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	PhotoItem photoItem = aibum.getBitList().get(position);
    	PhotoGridItem itemView = null;
        if (convertView==null||convertView.getTag()==null) {
            MyLog.e(TAG, "PhotoAdapter          convertView= " + convertView);
        	ViewHolder viewHolder = new ViewHolder();//实例viewHolder
        	itemView = new PhotoGridItem(mActivity,200,200);//初始化图片显示的大小
        	itemView.selectedPhotoImageView().setOnClickListener(this);//设置选择图片的按钮的点击事件
        	itemView.getImageView().setOnClickListener(this);//设置大图片的点击事件，用于跳转到ViewPager显示图片
        	viewHolder.mPhotoGridItem = itemView;//赋值给ViewHolder
            itemView.setTag(viewHolder);//给View设置ViewHolder

        } else {
            MyLog.d(TAG,"convertView   不为空，使用convertView缓存！ ");
        	ViewHolder viewHolder = (ViewHolder) convertView.getTag();
        	itemView = viewHolder.mPhotoGridItem;
        }
        MyImageFetcher.getInstance().loadImage(new LoadImageThumb(mActivity.getApplicationContext(),
        		Thumbnails.MINI_KIND, photoItem.getPhotoID(),200, 200), 
        		itemView.getImageView(),
        		R.drawable.default_center);
        boolean flag = photoItem.isSelect();
        itemView.setChecked(flag);
        itemView.selectedPhotoImageView().setTag(photoItem);
        itemView.getImageView().setTag(position);
        return itemView;
    }

    private class ViewHolder {
    	private PhotoGridItem mPhotoGridItem;
    }

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.iv_select_pic){//点击了选择图片
			PhotoItem photoItem = (PhotoItem) v.getTag();
			mActivity.setPhotoCheck(photoItem);
		}
		else if(v.getId() == R.id.photo_img_view){//点击了显示大图片的按钮，通过ViewPager形式展示相册
			int position = (Integer) v.getTag();
			final ArrayList<PictureData> pictures = new ArrayList<PictureData>();
            PictureData pictureData = null;
            List<PhotoItem> bitList = aibum.getBitList();
            for (int i = 0; i < bitList.size(); i++) {
            	PhotoItem item = bitList.get(i);
                pictureData = new PictureData();
                pictureData.setmUrl(item.getPhotoPath());
                pictureData.setmType("");
                pictureData.setIndex(i);
                pictureData.setUrlOrPath(PictureData.UrlOrPathStata.STATE_PATH);
                pictureData.setSelect(item.isSelect());;
                pictures.add(pictureData);
            }
            Intent intent = new Intent(mActivity, ViewPagerActivity.class);
            intent.putParcelableArrayListExtra(ViewPagerActivity.Param.INTENT_KEY_PICTURES, pictures);
            intent.putExtra(ViewPagerActivity.Param.INTENT_KEY_FLAG, ViewPagerActivity.Param.FLAG_CHOOSE_PHOTO);
            intent.putExtra(ViewPagerActivity.Param.INTENT_KEY_INDEX, position);
            intent.putExtra(ViewPagerActivity.Param.INTENT_KEY_CHOOSE_COUNT, mActivity.getSelectSize());
            intent.putExtra(ViewPagerActivity.Param.INTENT_KEY_MAX_CHOOSE_COUNT, mActivity.getMaxSelectSize());
            mActivity.startActivityForResult(intent, PhotoActivity.REQUEST_PHOTO_DETAIL);
		}
	}
}
